import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/upload_entity.dart';
import 'package:ls_work/a_mine/page/add_qualifications_page.dart';
import 'package:ls_work/a_mine/widget/me_cer_cell.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/util/app_navigator.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:ls_work/util/toast.dart';

import '../../util/flat_button.dart';



class MeHealthPage extends StatefulWidget {
  @override
  _MeHealthPageState createState() => _MeHealthPageState();
}

class _MeHealthPageState extends State<MeHealthPage> {
  List<Upload> dataList = [];

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      /// 接口请求
      _fectData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('健康证'),
        elevation: 0.5,
        actions: <Widget>[
          FlatsButton(
            onPressed: () {
              AppNavigator.push(
                  context,
                  AddQualificationsPage(
                    callBack: () {
                      _fectData();
                    },
                    type: 3,
                  ));
            },
            child: Text(
              '新增',
              style: TextStyles.textGray16,
            ),
          )
        ],
      ),
      body: ListView.separated(
        itemCount: dataList.length,
        itemBuilder: (BuildContext context, int index) {
          return MeCerCell(
            info: dataList[index],
            type: 3,
          );
        },
        separatorBuilder: (BuildContext context, int index) {
          return Divider();
        },
      ),
    );
  }

  _fectData() {
    var map = {
      'pictureType': 3,
    };
    HttpUtil.instance
        .getDio()
        .post('${HttpApi.staffPicture}?${requestParams(map)}')
        .then((r) {
      ///
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());

        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            PhotoEntity model = PhotoEntity.fromJson(map['data']);
            setState(() {
              dataList = model.rows??[];
              // for (Upload da in model.rows) {
              //   imgUrlList.add(
              //       HttpApi.baseUrl + '/commonAttachDown/download?id=' +
              //           '${da.fileId}');
              // }
            });
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }
}
